package cn.net.isir.core;

import cn.net.isir.entity.User;
import cn.net.isir.entity.UserInfo;
import cn.net.isir.service.UserInfoService;
import cn.net.isir.service.UserService;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author liuxubo
 * @filename ShiroPhoneAuthorizingRealm.java
 * @package cn.net.isir.core
 * @email lxb@isir.net.cn
 * @date 2019/01/07
 * @comment
 * @since 1.8
 */
public class ShiroPhoneAuthorizingRealm extends AuthorizingRealm {

    @Resource
    private UserInfoService userInfoService;
    @Resource
    private UserService userService;

    /**
     * Retrieves the AuthorizationInfo for the given principals from the underlying data store.  When returning
     * an instance from this method, you might want to consider using an instance of
     * {@link SimpleAuthorizationInfo SimpleAuthorizationInfo}, as it is suitable in most cases.
     *
     * @param principals the primary identifying principals of the AuthorizationInfo that should be retrieved.
     * @return the AuthorizationInfo associated with this principals.
     * @see SimpleAuthorizationInfo
     */
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
        return null;
    }

    /**
     * Retrieves authentication data from an implementation-specific datasource (RDBMS, LDAP, etc) for the given
     * authentication token.
     * <p/>
     * For most datasources, this means just 'pulling' authentication data for an associated subject/user and nothing
     * more and letting Shiro do the rest.  But in some systems, this method could actually perform EIS specific
     * log-in logic in addition to just retrieving data - it is up to the Realm implementation.
     * <p/>
     * A {@code null} return value means that no account could be associated with the specified token.
     *
     * @param token the authentication token containing the user's principal and credentials.
     * @return an {@link AuthenticationInfo} object containing account data resulting from the
     * authentication ONLY if the lookup is successful (i.e. account exists and is valid, etc.)
     * @throws AuthenticationException if there is an error acquiring data or performing
     *                                 realm-specific authentication logic for the specified <tt>token</tt>
     */
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
        String phone = (String) token.getPrincipal();
        UserInfo userInfo = new UserInfo();
        userInfo.setTel(phone);
        List<UserInfo> userInfoList = userInfoService.selectList(userInfo);
        if (userInfoList.size() > 0) {
            UserInfo info = userInfoList.get(0);
            User user = userService.selectByPrimaryKey(info.getUserId());
            if (user == null) {
                throw new UnknownAccountException("用户不存在");
            }
            if ("-1".equals(user.getStatus())) {
                throw new LockedAccountException("账户已锁定");
            }
            if ("0".equals(user.getStatus())) {
                throw new DisabledAccountException("账户已失效");
            }
            return new SimpleAuthenticationInfo(user.getUsername(), "OK", getName());
        }
        return null;
    }
}
